package cn.crm.controller;

import cn.crm.pojo.CrmDepartment;
import cn.crm.pojo.CrmStaff;
import cn.crm.service.DepartmentService;
import cn.crm.service.StaffService;
import cn.crm.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/staff")
public class StaffController {

    @Autowired
    private StaffService staffService;

    @Autowired
    private DepartmentService departmentService;

    //登陆验证
    @RequestMapping("/login")
    public String checkLogin(String loginName, String passWord, HttpServletRequest request){
        //调用service验证用户名密码是否正确
        CrmStaff staff = staffService.findUserAndPass(loginName, passWord);
        if(staff==null){
            //登陆失败
            request.setAttribute("msg","用户名或密码错误");
            return "login";
        }else{
            //登陆成功  把用户信息放入session ,显示系统首页
             request.getSession().setAttribute("yuangong",staff);
             return "frame";
        }

    }

    //分页展示员工信息
    @RequestMapping("/listStaff")
    public String  findStaffList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "1") int size,Model model){
       //得到所有部门
        List<CrmDepartment> allDepartments = departmentService.findAllDepartments();
        model.addAttribute("departments",allDepartments);

        PageUtil pageUtil = staffService.findStaffInfo(page, size);
        model.addAttribute("pageUtil",pageUtil);

        return "listStaff";
    }
    //多条件查询
    @RequestMapping("/listStaffs")
    public String findStaffByCondition(@RequestParam(defaultValue = "1") int page,@RequestParam(defaultValue = "1") int size,CrmStaff crmStaff,Model model){
       //得到所有部门
        List<CrmDepartment> allDepartments = departmentService.findAllDepartments();
        model.addAttribute("departments",allDepartments);


        PageUtil pageUtil = staffService.findStaffByCondition(page, size, crmStaff);
        model.addAttribute("pageUtil",pageUtil);

        model.addAttribute("name",crmStaff.getStaffname());
        return "listStaff";
    }

    @RequestMapping("/editStaff")
    public String showStaffInfo(String staffId,Model model){
        //得到所有部门
        List<CrmDepartment> allDepartments = departmentService.findAllDepartments();
        model.addAttribute("departments",allDepartments);

        //调用service，根据staffId查询员工信息
        CrmStaff staff = staffService.findStaffById(staffId);
        model.addAttribute("staff",staff);

        return "editStaff";
    }

    //秀给员工信息
    @RequestMapping("/updateStaff")
    public String updateStaff(CrmStaff crmStaff){
        //调用service的修改功能
        staffService.updateStaffInfo(crmStaff);

        return "redirect:/staff/listStaff";
    }

    //转到添加员工页面
    @RequestMapping("/addStaffPage")
    public String toAddPage(Model model){
        //得到所有部门
        List<CrmDepartment> allDepartments = departmentService.findAllDepartments();
        model.addAttribute("departments",allDepartments);

        return "addStaff";
    }

    @RequestMapping("/addStaff")
    public String addStaff(CrmStaff crmStaff){
        //调用service的添加功能
        staffService.addStaff(crmStaff);
        return "redirect:/staff/listStaff";
    }
}
